package online.shenme.gamestone;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {
    //活动图标对象
    JLabel[][] jLabels = new JLabel[4][4];
    //胜利状态对象
    JLabel[][] jLabelsV = new JLabel[4][4];
    int row, column;//空白图标坐标
    int count;

    //创建一个游戏运行框架
    public GameJFrame() {
        initWindows();
        initMenu();
        paintView();
        random();
        setVisible(true);

        addKeyListener(this);

    }

    //窗口工具栏绘制
    public void initMenu() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("菜单");
        JMenuItem item = new JMenuItem("重新开始");
        menu.add(item);
        menuBar.add(menu);
        setJMenuBar(menuBar);
        item.addActionListener(this);
    }

    //游戏窗口初始化
    public void initWindows() {
        setSize(514, 595);
        setLayout(null);
        setTitle("石头迷阵");
        setLocationRelativeTo(null);
        setAlwaysOnTop(true);
        setDefaultCloseOperation(3);
    }


    JLabel nums = new JLabel("步数：" + count);

    //游戏界面绘制
    public void paintView() {
        nums.setBounds(50, 20, 100, 20);
        getContentPane().add(nums);
        //绘制数字对象
        for (int i = 0; i < jLabels.length; i++) {
            for (int j = 0; j < jLabels[i].length; j++) {
                jLabels[i][j] = new JLabel(new ImageIcon("X:\\OneDrive - stu.cdutcm.edu.cn\\06技术传播\\行业研究\\IT\\Java\\项目\\image\\" + (i * 4 + j) + ".png"));
                getContentPane().add(jLabels[i][j]);
                jLabelsV[i][j] = jLabels[i][j];
            }
        }
        //设置坐标
        reBounds();
        //绘制背景
        JLabel background1 = new JLabel(new ImageIcon("X:\\OneDrive - stu.cdutcm.edu.cn\\06技术传播\\行业研究\\IT\\Java\\项目\\image\\background.png"));
        background1.setBounds(26, 30, 450, 484);
        getContentPane().add(background1);
    }
    //打乱组件对象
    public void random() {
        Random random = new Random();
        for (int i = 0; i < jLabels.length; i++) {
            for (int j = 0; j < jLabels[i].length; j++) {
                //随机产生两个索引值
                int indexi = random.nextInt(jLabels.length);
                int indexj = random.nextInt(jLabels[i].length);
                JLabel temp = jLabels[i][j];
                jLabels[i][j] = jLabels[indexi][indexj];
                jLabels[indexi][indexj] = temp;
                if (jLabels[i][j] == jLabelsV[0][0]) {
                    row = i;
                    column = j;
                }
                reBounds();
            }
        }
    }

    //遍历组件，根据索引设置坐标
    public void reBounds() {
        for (int i = 0; i < jLabels.length; i++) {
            for (int j = 0; j < jLabels[i].length; j++) {
                jLabels[i][j].setBounds(j * 100 + 50, i * 100 + 90, 100, 100);
            }
        }
    }

    //游戏胜利判定
    public boolean victory() {
        for (int i = 0; i < jLabels.length; i++) {
            for (int j = 0; j < jLabels[i].length; j++) {
                if (jLabels[i][j] != jLabelsV[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    //数字移动逻辑
    private void move(int key) {
        if (victory()) {
            //清除当前面板设置
            getContentPane().removeAll();
            //设置胜利置顶图片
            JLabel winIcon = new JLabel(new ImageIcon("X:\\OneDrive - stu.cdutcm.edu.cn\\06技术传播\\行业研究\\IT\\Java\\项目\\image\\win.png"));
            winIcon.setBounds(124, 230, 266, 88);
            getContentPane().add(winIcon);
            //重置面板对象为胜利状态
            paintView();
            //重新加载面板组件
            getContentPane().repaint();
            return;
        } else if (key == 37) {
            if (column + 1 < 4) {
                JLabel temp = jLabels[row][column];
                jLabels[row][column] = jLabels[row][column + 1];
                jLabels[row][column + 1] = temp;
                column++;
            }
        } else if (key == 38) {
            if (row + 1 < 4) {
                JLabel temp = jLabels[row][column];
                jLabels[row][column] = jLabels[row + 1][column];
                jLabels[row + 1][column] = temp;
                row++;
            }
        } else if (key == 39) {
            if (column - 1 >= 0) {
                JLabel temp = jLabels[row][column];
                jLabels[row][column] = jLabels[row][column - 1];
                jLabels[row][column - 1] = temp;
                column--;
            }
        } else if (key == 40) {
            if (row - 1 >= 0) {
                JLabel temp = jLabels[row][column];
                jLabels[row][column] = jLabels[row - 1][column];
                jLabels[row - 1][column] = temp;
                row--;
            }
        } else if (key == 90) {
            paintView();
            move(key);
        }
        reBounds();

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        move(keyCode);
        count++;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        count = 0;
        new GameJFrame();
    }

    //---------不使用
    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }


}

